class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        s = " " + s, p = " " + p;
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
        // dp[i][j] 表示： p字符串[0, j]区间内的子串能否匹配字符串s的[0, i]区间内的子串
        dp[0][0] = true;
        for(int j = 1; j <= n; ++j)
        {
            if(p[j] == '*')
                dp[0][j] = true;
            else
                break;
        }
 
        for(int i = 1; i <= m; ++i)
        {
            for(int j = 1; j <= n; ++j)
            {
                if(s[i] == p[j] || p[j] == '?')
                    dp[i][j] = dp[i - 1][j - 1];
                else if(p[j] == '*')
                    dp[i][j] = (dp[i][j - 1] || dp[i - 1][j]);
            }
        }
        return dp[m][n];
    }
};